from django.shortcuts import render, HttpResponse, redirect, reverse
from user.models import User
from course.models import Course
from course import views as course_views


# Create your views here.
def index_handler(request):
    context = request.context
    session_user = request.session['session_user']
    user = User.objects.get(id=session_user.get('id'))
    context['user'] = user
    if request.method == 'GET':
        return render(request, 'user.html', context)
    else:
        user.username = request.POST.get('username')
        user.gender = request.POST.get('gender')
        user.tel = request.POST.get('tel')
        user.save()
        return redirect(reverse('user:index'))
        #return render(request, 'user.html', context)

def course_handler(request):
    context = request.context
    session_user = request.session['session_user']

    if not session_user:
        context['message'] = '需要登录'
        return render(request, "user_message.html", context)

    try:
        user = User.objects.get(id=session_user.get('id'))
        course_s = user.userBuyer_set.all()
        context['course_s'] = course_s
        return render(request, 'user_course.html', context)
    except:
        context['message'] = '服务器错误'
        return render(request, 'user_message.html', context)


def shoppingCart_handler(request):
    context = request.context
    session_user = request.session['session_user']

    if not session_user:
        return HttpResponse(status=403)

    user = User.objects.get(id=session_user.get('id'))
    course_s = user.userShoppingcart_set.all();
    context['course_s'] = course_s

    return render(request, 'user_shoppingcart.html', context)


def login_handler(request):
    if 'POST' != request.method:
        return HttpResponse(status=403)

    context = request.context
    account = request.POST.get('account')
    password = request.POST.get('password')

    try:
        user_s = User.objects.filter(account=account, password=password)
        if not user_s:
            context['login_message'] = '登录失败'
        else:
            request.session['session_user'] = {'id': user_s[0].id, 'account': user_s[0].account}
            context['session_user'] = {'id': user_s[0].id, 'account': user_s[0].account}
            request.session.set_expiry(3600)
            return redirect(reverse('course:index'))
    except:
        context['login_message'] = '账号或密码错误'
    finally:
        return course_views.index_handler(request)


def register_handler(request):
    if request.method != 'POST':
        return HttpResponse(status=403)

    context = request.context

    account = request.POST.get('account')
    pwd = request.POST.get('password')

    try:
        userexists = User.objects.filter(account=account).exists()
        if userexists:
            context['register_message'] = '账号已存在'
        else:
            u = User(account=account, password=pwd)
            u.save()
            request.session['session_user'] = {'id': u.id, 'account': u.account}
            request.session.set_expiry(3600)
    except:
        context['register_message'] = '服务器异常'
    finally:
        # return course_views.index_handler(request)
        return redirect(reverse('course:index'))


def logout_handler(request):
    request.session['session_user'] = None
    return redirect(reverse('course:index'))


def purchase_handler(request, course_id):
    context = request.context
    session_user = request.session['session_user']

    if not session_user:
        context['message'] = '购买课程先登录'
    else:
        user = User.objects.get(id=session_user.get('id'))
        course = Course.objects.get(id=course_id)
        if User.objects.filter(id=session_user.get('id'), userBuyer_set__id=course_id).exists():
            context['message'] = '已经购买，不需要重复购买'
            try:
                user.userShoppingcart_set.remove(course)
                user.save()
            except:
                pass
        else:
            if user.money < course.price:
                context['message'] = '余额不足'
            else:
                user.userBuyer_set.add(course)
                user.save()
                user.money -= course.price
                user.save()
                try:
                    user.userShoppingcart_set.remove(course)
                    user.save()
                except:
                    pass

                context['message'] = '购买成功'

    return render(request, 'user_message.html', context)

def addShoppingCart_handler(request, course_id):
    context = request.context
    session_user = request.session['session_user']

    if None == session_user:
        context['message'] = '添加购物车需要先登录'
    else:
        try:
            course = Course.objects.get(id=course_id)
            user = User.objects.get(id=session_user.get('id'))
            course.userShoppingcart.add(user)
            course.save()
            context['message'] = '添加购物车成功'
        except:
            context['message'] = '添加购物车失败'

    return render(request, 'user_message.html', context)
